const conn = require("../data");
// 新闻 数据渲染
module.exports.news_show = (req, res) => {
  pageNum = req.query.pageNum;
  pageSize = req.query.pageSize;
  index = (pageNum - 1) * pageSize;
  console.log(pageNum, pageSize, index, req.query.href);
  if (req.query.href == "/news" || req.query.href == "/org") {
    console.log(111);
    conn.query(
      `select count(*) total from page_news where page_options_id in (select id from pc_nav2 where nav_id = (select id from pc_nav2 where href=?));select * from page_news where page_options_id in (select id from pc_nav2 where nav_id = (select id from pc_nav2 where href=?)) limit ${index},${pageSize}`,
      [req.query.href, req.query.href],
      (err, data) => {
        if (err) return console.log(err);
        for (i = 0; i < data[1].length; i++) {
          text = "";
          for (j = 0; j < 80; j++) {
            text += data[1][i].matter[j];
          }
          data[1][i].matter = text;
        }
        res.json({
          pageSize: pageSize,
          data: data[1],
          total: data[0][0].total
        });
      }
    );
  } else {
    conn.query(
      `select count(*) total from page_news where page_options_id = (select id from pc_nav2 where href=?);select * from page_news where page_options_id = (select id from pc_nav2 where href=?) limit ${index},${pageSize}`,
      [req.query.href, req.query.href],
      (err, data) => {
        if (err) return console.log(err);
        for (i = 0; i < data[1].length; i++) {
          text = "";
          for (j = 0; j < 80; j++) {
            text += data[1][i].matter[j];
          }
          data[1][i].matter = text;
        }
        res.json({
          pageSize: pageSize,
          data: data[1],
          total: data[0][0].total
        });
      }
    );
  }
};
